package yxy.game.pm2.command.moudleactive.rankprogress;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ModuleActiveMessage;
import yxy.game.pm2.module.active.progress.RankProgress;
import yxy.game.pm2.module.active.progress.RankProgressArea;
import yxy.game.pm2.module.active.progress.RankProgressPerson;
import yxy.game.pm2.module.player.PlayerHandle;
import yxy.game.pm2.module.state.RewardState;

import java.util.ArrayList;
import java.util.HashMap;

/** 模块活动 排行榜进度 */
@MessageType(proto = Command.RankProgressRewardInfo, classtype = ModuleActiveMessage.RankProgressRewardInfoRequest.class)
public class CMD_RankProgressRewardInfo extends Command<ModuleActiveMessage.RankProgressRewardInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ModuleActiveMessage.RankProgressRewardInfoRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();
		ModuleActiveMessage.RankProgressRewardInfoResponse.Builder response = ModuleActiveMessage.RankProgressRewardInfoResponse.newBuilder();
		response.setType(type);
		
		RankProgressArea moudlearea = RankProgressArea.get(playerCode, type);
		RankProgressPerson moudleperson = RankProgressPerson.get(playerCode, type);
		
		ArrayList<String> newcodes = new ArrayList<String>();
		
		HashMap<Integer, RankProgress> rpmap = moudlearea.getRpmap();
		for(int id : rpmap.keySet()) {
			ModuleActiveMessage.RankFinishInfo.Builder build = ModuleActiveMessage.RankFinishInfo.newBuilder();
			
			RankProgress rp = rpmap.get(id);
			
			build.setId(id);
			build.setFinishplayercode(rp.getPlayercode());
			
			int state = moudleperson.getDrawids().contains(id) ? RewardState.HadDraw.getState() : RewardState.CanDraw.getState();
			build.setState(state);
			
			response.addRfinfos(build.build());
			
			if(newcodes.contains(rp.getPlayercode())) {
				continue;
			}
			newcodes.add(rp.getPlayercode());

		}
		
		PlayerHandle playerHandle = new PlayerHandle();
		for(String code : newcodes) {
			response.addPsiminfos(playerHandle.playerSimpleInfo(code));
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ModuleActiveMessage.RankProgressRewardInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}